package com.xmut.mall.member.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.xmut.mall.member.annotation.CheckJwt;
import com.xmut.mall.member.utils.RespBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Id;
import org.springframework.web.bind.annotation.*;

import com.xmut.mall.member.entity.MemberReceiveAddressEntity;
import com.xmut.mall.member.service.MemberReceiveAddressService;
import com.xmut.mall.member.utils.PageUtils;
import com.xmut.mall.member.utils.R;



/**
 * 会员收货地址
 *
 * @author 1912120131suntingxing
 * @email 1192081604@qq.com
 * @date 2022-07-14 01:20:45
 */
@RestController
@RequestMapping("member/memberreceiveaddress")
@Slf4j
public class MemberReceiveAddressController {
    @Autowired
    private MemberReceiveAddressService memberReceiveAddressService;

    /**
     * 设置默认收货地址
     * @param params
     * @return
     */
    @CheckJwt
    @PutMapping("/setDefaultAddress")
    public RespBean setDefaultAddress(@RequestBody Map<String,Object> params){
        return memberReceiveAddressService.setDefaultAddress(params);
    }

    /**
     * 删除用户的收货地址
     * @param id
     * @return
     */
    @CheckJwt
    @DeleteMapping("/deleteUserReceiveAddress/{id}")
    public RespBean deleteUserReceiveAddress(@PathVariable Long id){
        return memberReceiveAddressService.deleteUserReceiveAddress(id);
    }

    /**
     * 修改用户收货地址的详细数据
     * @param address
     * @return
     */
    @CheckJwt
    @PutMapping("/updateUserReceiveAddress")
    public RespBean updateUserReceiveAddress(@RequestBody MemberReceiveAddressEntity address){
        Boolean result = memberReceiveAddressService.updateUserReceiveAddress(address);
        if(result){
            return RespBean.success("修改成功");
        }else {
            return RespBean.error("修改失败，请重试");
        }
    }

    /**
     * 获取用户的默认收货地址
     * @param id
     * @return
     */
    @CheckJwt
    @GetMapping("/getDefaultReceiveAddressById/{id}")
    public RespBean getDefaultReceiveAddressById(@PathVariable Long id){
        MemberReceiveAddressEntity address = memberReceiveAddressService.getDefaultReceiveAddressById(id);
        if(address!=null){
            return RespBean.success("获取地址成功",address);
        }else {
            return RespBean.error("网络延迟");
        }
    }

    /**
     * 根据id获取用户收货地址
     * @param id
     * @return
     */
    @CheckJwt
    @GetMapping("/getReceiveAddressById/{id}")
    public RespBean getReceiveAddressById(@PathVariable Long id){
        MemberReceiveAddressEntity address = memberReceiveAddressService.getReceiveAddressById(id);
        if(address!=null){
            return RespBean.success("获取地址成功",address);
        }else {
            return RespBean.error("网络延迟");
        }
    }

    /**
     * 根据用户id获取用户的收货地址
     * @param userId
     * @return
     */
    @CheckJwt
    @GetMapping("/getUserReceiveByUserId/{userId}")
    public RespBean getUserReceiveByUserId(@PathVariable Long userId){
        List<MemberReceiveAddressEntity> list = memberReceiveAddressService.getUserReceiveByUserId(userId);
        return RespBean.success("获取用户收货地址成功",list);
    }


    /**
     * 用户添加收货地址
     * @param address
     * @return
     */
    @CheckJwt
    @PostMapping("/addUserReceive")
    public RespBean addUserReceive(@RequestBody MemberReceiveAddressEntity address){
        return memberReceiveAddressService.addUserReceive(address);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberReceiveAddressService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		MemberReceiveAddressEntity memberReceiveAddress = memberReceiveAddressService.getById(id);

        return R.ok().put("memberReceiveAddress", memberReceiveAddress);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MemberReceiveAddressEntity memberReceiveAddress){
		memberReceiveAddressService.save(memberReceiveAddress);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MemberReceiveAddressEntity memberReceiveAddress){
		memberReceiveAddressService.updateById(memberReceiveAddress);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		memberReceiveAddressService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
